package whiteboard

import android.content.Context
import android.graphics.Color
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.RelativeLayout

abstract class GraView(ctxt: Context, width: Int, height: Int): RelativeLayout(ctxt){
    val gra = Graffiti(ctxt, width, height)

    abstract fun exitGra()

    init {
        gra.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        val linearLayout = LinearLayout(ctxt)

        val png = arrayOf(
            R.drawable.undo,
            R.drawable.redo,
            R.drawable.clear,
            R.drawable.line,
            R.drawable.rect,
            R.drawable.circle,
            R.drawable.red,
            R.drawable.blue,
            R.drawable.orange,
            R.drawable.width1,
            R.drawable.width2,
            R.drawable.width3,
            R.drawable.exit
        )

        val btn = arrayOf(
            Button(ctxt),
            Button(ctxt),
            Button(ctxt),
            Button(ctxt),
            Button(ctxt),
            Button(ctxt),
            Button(ctxt),
            Button(ctxt),
            Button(ctxt),
            Button(ctxt),
            Button(ctxt),
            Button(ctxt),
            Button(ctxt)
        )

        val event = arrayOf(
            {gra.undo()},
            {gra.redo()},
            {gra.clear()},
            {gra.type = Graffiti.Type.LINE},
            {gra.type = Graffiti.Type.RECT},
            {gra.type = Graffiti.Type.CIRCLE},
            {gra.pColor = Color.RED},
            {gra.pColor = Color.BLUE},
            {gra.pColor = Color.parseColor("#FF8F00")},
            {gra.pWidth = 3f},
            {gra.pWidth = 6f},
            {gra.pWidth = 9f},
            {exitGra()}
        )

        linearLayout.run {
            layoutParams = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
            orientation = LinearLayout.VERTICAL
        }

        val size = height / 13

        btn.forEachIndexed { i, it -> it.run{
                setBackgroundResource(png[i])
                layoutParams = ViewGroup.LayoutParams(size, size)
                setOnClickListener { event[i]() }
            }
            linearLayout.addView(it)
        }

        this.addView(gra)
        this.addView(linearLayout)
    }

}